/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package drinkcounter.web.servlet;

import drinkcounter.util.PartyMarshaller;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import drinkcounter.DrinkCounterService;
import drinkcounter.model.Participant;
import drinkcounter.util.PathInfoHelper;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Toni
 */
@Singleton
public class PartyAPIServlet extends HttpServlet{

    private static final Logger log = LoggerFactory.getLogger(PartyAPIServlet.class);

    @Inject private DrinkCounterService service;
    @Inject private PartyMarshaller partyMarshaller;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doRequest(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doRequest(req, resp);
    }

    private void doRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = getAction(req);
        log.debug("action: "+action);
        if(StringUtils.isBlank(action)){
            printXml(req, resp);
        }else if("add-drink".equals(action)){
            addDrink(req, resp);
        }else if("add-participant".equals(action)){
            addParticipant(req,resp);
        }else if("remove-participant".equals(action)){
            removeParticipant(req, resp);
        }
        else{
            resp.sendError(404);
        }
        
    }

    private String getAction(HttpServletRequest req){
        String pathInfo = req.getPathInfo();
        String[] parameters = PathInfoHelper.splitPathInfo(pathInfo);
        if(parameters.length < 2){
            return "";
        }else{
            return parameters[1];
        }
        
    }
    private String getPartyId(HttpServletRequest req){
        String pathInfo = req.getPathInfo();
        String[] parameters = PathInfoHelper.splitPathInfo(pathInfo);
        return parameters[0];
    }

    private void printXml(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/xml");
        OutputStream out = resp.getOutputStream();
        try {
            String partyId = getPartyId(req);
            partyMarshaller.marshall(partyId, out);
        } catch (Exception ex) {
            log.error("Error building xml response", ex);
        }finally{
            out.close();
        }
    }

    private void addDrink(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String participantIdentifier = req.getParameter("participant-id");
        service.addDrink(participantIdentifier);
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();
        out.print(service.getDrinks(participantIdentifier, null, null).size());
        out.close();
    }

    private void addParticipant(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String partyId = getPartyId(req);

        Participant participant = ParticipantHelper.parseNewParticipant(req);
        participant = service.addParticipant(participant, partyId);
        resp.setContentType("text/plain");
        resp.setCharacterEncoding("UTF-8");
        PrintWriter out = resp.getWriter();
        out.print(participant.getId());
        out.close();
    }

    private void removeParticipant(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String participantId = req.getParameter("participant-id");
        service.deleteParticipant(participantId);
        resp.getOutputStream().close();
    }
}
